/*
 * Relatorios.java
 *
 * Created on 14 de Junho de 2009, 19:16
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package necticadastro;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import javax.crypto.NullCipher;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.view.JasperViewer;
import java.util.*;

/**
 *
 * @author Netto
 */
public class Relatorios {
  private Connection conexao;
  private Statement statement;
  private String query;
  private ResultSet resultSet;
  private JRResultSetDataSource jrRS;
  public Map parameters;
  private JasperReport relatorio;
  private JasperPrint printRelatorio;
  private String nomeRelatorio;
  private static BDConnection dbConexao;
  private String report_path = "";
  private Logger log = null;
  
  /** Creates a new instance of Relatorios 
   *@param PTipo: False se for para visualizar e True se for para imprimir
   */
  public void geraRelatorio(boolean PTipo) throws JRException, Exception
  {
      //resultSet = (ResultSet) statement.executeQuery(query);
      //jrRS = new JRResultSetDataSource( resultSet );
      
      report_path = System.getProperty("user.dir") + "\\relatorios";
      //relatorio = JasperCompileManager.compileReport(nomeRelatorio);
      parameters.put("report_path", report_path);
      printRelatorio = JasperFillManager.fillReport(report_path.replace('\\', '/') + "/" + nomeRelatorio, parameters, conexao);

      if (!PTipo)
        JasperViewer.viewReport(printRelatorio, false);
      else
        JasperPrintManager.printReport(printRelatorio, true);
      
  }

  public Relatorios() {
      log = Logger.getInstance();
        try {
            dbConexao = new BDConnection();
            conexao = dbConexao.getConnection();
            statement = (Statement) conexao.createStatement();
            parameters = new HashMap();
        } catch (Exception ex) {
            ex.printStackTrace();
            log.LogException(ex);
        }
      query     = "";
  }
  
  public String getQuery() {
      return query;
  }
    
  public void setNomeRelatorio(String PRelatorio) {
      if ( (PRelatorio != null) && (!PRelatorio.trim().equals("")) ) {
          nomeRelatorio = PRelatorio;
      }
  }
  
  public void setConexao(Connection PConexao) {
      if (PConexao != null) {
          conexao = PConexao;
      }
  }
    
  public void setStatement(Statement PStatement) {
      if (PStatement != null) {
          statement = PStatement;
      }
  }

  public void setResultSet(ResultSet PResultSet) {
      if (PResultSet != null) {
          resultSet = PResultSet;
      }
  }

  public void setQuery(String PQuery) {
      if (PQuery != null) {
          query = PQuery;
      }
  }
}
